import React from 'react';
import {
	Button,
	Group,
	TextareaField,
	Toast,
	SelectField,
	CheckboxField,
} from 'saltui';
const {
	Count
} = TextareaField;
import PageController from 'pages/home';
import {
	urlPrefix,
	URLS,
	nodataIcon,
	dictMap
} from '../../../app/variables';
import DB from '../../../app/db';

import Util from 'components/common/util';
import Upload from 'components/upload';
let boradNewArray=[];
export default class PageHome extends React.Component {

	constructor(props) {
		
		super(props);
		
		this.postId = props.params.postId;

		this.state = {	
			id: this.postId,
			boradArray: [],
			
		}

			dd.biz.navigation.setTitle({
      		title: '选择标签',
		    onSuccess: function (result) {
		    },
     		 onFail: function () {
      		}
    		});
		
		

	}

	componentWillMount() {
		boradNewArray=[];
		// debugger;
		if (this.postId) {
			this.allLabel();
		}

	}

	componentDidMount() {

	}


	allLabel(){
		
		DB.ForumModuleAPI.postAllLabel({
			id: this.postId
		}).then((content) => {
			Toast.hide();
			if (content && content.length > 0) {
				let array = [];
				content.map((item, index) => {
					let i = {
						text: item.name,
						value: item.id,
						checked:item.checked,
					}
					array.push(i);
				});
				
				this.setState({
					boradArray: array,
					
				})
			}
		}).catch((error) => {

		});
	}


	//去查询所有的标签
	getCheckboxFieldProps() {
    return {
      data: this.state.boradArray,
      readOnly: false,
      placeholder: '请选择',
      maskCloseable: true,
      groupListFlag: true,
      onChange(data) {
        boradNewArray=data;
      },
      mode: 'list',
      required: true,
      tip: <div>这里是提示信息</div>
    }
	}

	

	/**
	 * 表单提交  2中情况，2个都为【】的时候说明没有做任何选择，还有就是新的为【】，
	 原来的有值，要么是取消了，要么是没有变化，这时候把老的值赋给新的
	 * @return {[type]}
	 */
	handleSubmit() {

		let labelArray=[];
		let labelNewArray=[];
		labelArray=this.state.boradArray;
		labelNewArray=boradNewArray;
		
		let idArray = '';
		let newIdArray = '';
		if(labelArray.length>=1){
			for(var i=0;i<labelArray.length;i++){
				let labelCkeck=labelArray[i].checked;
				let labelId=labelArray[i].value;
				if(labelCkeck==true){
					idArray=idArray+labelId+'|';
				}
			}
		}

		if(labelNewArray.length>=1){
			for(var i=0;i<labelNewArray.length;i++){
				var labelNewId=labelNewArray[i].value;
				newIdArray=newIdArray+labelNewId+'|';
			}
		}

		/*if((idArray==undefined || idArray=="") && (newIdArray==undefined || newIdArray=="")){
			 Toast.show({
          type: 'error',
          content: '未选择任何标签！',
          onHide: function () {}
        });
        return false;
		}
*/
      	
	
		let entity = {
			postId:this.postId,
			idArray:idArray,
			newIdArray:newIdArray
		};

		//如果超过六个标签
		if(labelNewArray.length>6){
			Toast.show({
				type: 'error',
				content: '没有位置了，请先移出不常用的标签后重新添加'
			});
			return;
 		}
		DB.ForumModuleAPI.editPostLabel({
			postId:this.postId,
			idArray:idArray,
			newIdArray:newIdArray
		}).then((content) => {
			Toast.hide();
			Toast.show({
				type: 'success',
				content: '标记成功'
			});
			history.go(-1);
			window.salt.router.goBack();
		}).catch((error) => {
			if (typeof(error.errorMsg) == 'undefined') {
				Toast.show({
					type: 'error',
					content: '标签选择出错'
				});
			} else {
				Toast.show({
					type: 'error',
					content: error.errorMsg
				});
			}
		});
	}

	/**
	 * 取消提交表单
	 * @return {[type]}
	 */
	handleCancel() {
		history.go(-1);
	}

	render() {
    const CheckboxFieldProps = this.getCheckboxFieldProps();
    return (
      <div className="checkbox-field-demo">
        <Group className="checkbox-field-demo-group">
          <Group.List>
          	<div className="t-PR6 t-PL6">
          		<CheckboxField {...CheckboxFieldProps} />
          	</div>
          </Group.List>
        </Group>


         <div>
		<Button.Group className="page-add">					
		  <Button type="primary" display="inline"
		         onClick={this.handleSubmit.bind(this)}>确定</Button>
		  <Button type="secondary" display="inline"
		         onClick={this.handleCancel.bind(this)}>取消</Button>
		</Button.Group>
	</div>	
      </div>

     
    );
  }
}